/**=========================================================
 * Module: utils.js
 * create by LDM
 * 2018-06-21
 =========================================================*/


(function ($, window) {
  'use strict';

  var $win = $(window);
  $.Utils = $.Utils || {};
  var isPublish = true;  //备注:部署服务器的时候,设置成true- //

  // 关闭日志
  $.Utils._orgConsoleLog = console.log;
  isPublish ? (console.log = () => {
  }) : '';


  let winWidth = document.documentElement.clientWidth;
  let winHeight = document.documentElement.clientHeight;
  /**
   * 监听窗口大小改变
   */
  $.Utils.resize = () => {
    let newWinW = document.documentElement.clientWidth;
    let newWinH = document.documentElement.clientHeight;
    if (Math.abs(winWidth - newWinW) > 50 || Math.abs(winHeight != newWinH) > 50)
      window.top.location.reload();
  };
  /**
   * 飘字
   * @param textContent
   * @param secondNum
   * @param cb
   */
  $.Utils.showBarrage = (textContent, secondNum, cb) => {
    if (textContent == null || textContent == '') {
      return;
    }
    let barrageHtml = "";
    barrageHtml += '<div class="barrage" style="position: fixed; top:45%; left:0;width: 100%;text-align: center;z-index: 99999;">';
    barrageHtml += '<div class="barrage-info" style="font-size:14px;min-width:60%;border-radius: 10px;padding: 10px 15px;text-align: center;color: #FFFFFF;background: rgba(0, 0, 0, 0.4);display: inline-block">';
    barrageHtml += '<span class="barrage_text">' + textContent + '</span>';
    barrageHtml += '</div>';
    barrageHtml += '</div>';
    if ($("body").find(".barrage").length > 0) {
      $(this).remove();
    } else {
      $("body").append(barrageHtml);
    }
    $('.barrage').click(function() {
      $(this).remove();
    });
    let second = secondNum || 2000;
    $(".barrage .barrage-info").find(".barrage_text").text(textContent);
    setTimeout(() => {
      if ($(".barrage").length > 0) {
        $('.barrage').remove();
      }
      cb && cb();
    }, second)
  };

  /**
   * 判断用户是否退出登录
   * @param data
   * @param cb
   */
  $.Utils.checkError = (data, cb) => {
    if (data.code == "err_not_auth" || data.code == "err_not_login") {
      $.Utils.showBarrage(data.msg || "请重新登录", 1000, () => {
        window.top.location.href = "../../index.html";
      });
    } else if (data.code != 'success' && data.msg) {
      $.Utils.showBarrage(data.msg);
    }
    cb && cb();
  };
  /**
   * 判断是不是ie10
   * @returns {boolean}
   */
  $.Utils.isIE10 = () => {
    let navi = window.navigator.userAgent.toLowerCase();
    return navi.indexOf("msie 10") != -1;
  };

  Date.prototype.format = (format) => {
    let date = {
      "M+": this.getMonth() + 1,
      "d+": this.getDate(),
      "h+": this.getHours(),
      "m+": this.getMinutes(),
      "s+": this.getSeconds(),
      "q+": Math.floor((this.getMonth() + 3) / 3),
      "S+": this.getMilliseconds()
    };
    if (/(y+)/i.test(format)) {
      format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (let k in date) {
      if (new RegExp("(" + k + ")").test(format)) {
        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
      }
    }
    return format;
  };
  /**
   * 对时间戳转换成时间格式
   * @param data
   * @param style
   * @returns {*}
   */
  $.Utils.setDateFormat = (data, style) => {
    let date = new Date(data);
    style = style ? style : 'yyyy-MM-dd';
    return date.format(style);
  };
  $.Utils.toIntegerFunc = function (data, isOk) {
    let y = (data - parseInt(data)) * 10 >= 5 ? 1 : 0;
    data = isOk ? parseInt(data + y) : parseInt(data);
    return data;
  };


  $.Utils.isWeixinCheck = () => {
    let ua = navigator.userAgent.toLowerCase();
    let isWeixin = ua.indexOf('micromessenger') != -1;
    let isAndroid = ua.indexOf('android') != -1;
    let isIos = (ua.indexOf('iphone') != -1) || (ua.indexOf('ipad') != -1);
    if (!isWeixin) {
      document.head.innerHTML = '<title>抱歉，出错了</title>' +
        '<meta charset="utf-8">' +
        '<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">' +
        '<link rel="stylesheet" type="text/css" href="https://res.wx.qq.com/open/libs/weui/0.4.1/weui.css">';
      document.body.innerHTML = '<div class="weui_msg">' +
        '<div class="weui_icon_area">' +
        '<i class="weui_icon_info weui_icon_msg"></i>' +
        '</div>' +
        '<div class="weui_text_area">' +
        '<h4 class="weui_msg_title">请在微信客户端打开链接</h4>' +
        '</div>' +
        '</div>';
    }
  };
  // $.Utils.isWeixinCheck();
  $.Utils.getCookie = function (name) {
    let strcookie = document.cookie;//获取cookie字符串
    let arrcookie = strcookie.split("; ");//分割
    //遍历匹配
    for (let i = 0; i < arrcookie.length; i++) {
      let arr = arrcookie[i].split("=");
      if (arr[0] == name) {
        return arr[1];
      }
    }
    return "";
  };
  $.Utils.getUserOpenid = function () {
    return unescape($.Utils.getCookie("useropenid"));
  };

  $.Utils.decodeData = function (data) {
    if (typeof data == "object") {
      for (let i in data) {
        data[i] = decodeURIComponent(data[i]);
      }
    } else {
      data = decodeURIComponent(data);
    }
    return data;
  }


}(jQuery, window));
